/* *****************************************************************************************
 
Made by www.Flashphoner.com, all rights reserved.
These sources can only be used for working with Flashphoner server.
If you want use this to create your own application (separate from Flashphoner server) � 
please contact support@flashphoner.com 

******************************************************************************************/

package com.flashphoner.api
{	
	
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.flashphoner.Logger;
	import com.flashphoner.api.data.ErrorCodes;
	import com.flashphoner.api.data.ModelLocator;
	import com.flashphoner.api.data.PhoneConfig;

	public class PhoneCallback
	{	
		private var flash_API:Flash_API;
		public function PhoneCallback(flashAPI:Flash_API)
		{
			this.flash_API = flashAPI; 
		}
		
		public function notifyBalance(balance:Number,_sipObject:Object):void{
			flash_API.apiNotify.notifyBalance(balance,_sipObject);

		}	
		
		public function getVersion(version:String):void{
			PhoneConfig.VERSION_OF_SERVER = version;
		}
		
		public function getUserData(resObj:Object):void{
			var modelLocator:ModelLocator = flash_API.modelLocator;
			modelLocator.login = resObj.login;
			modelLocator.pwd = resObj.password;
			modelLocator.sipProviderAddress = resObj.sipProviderAddress;
			modelLocator.sipProviderPort = resObj.sipProviderPort;
			
		}
		
		public function fail(errorCode:String,_sipObject:Object):void{
			Logger.info("PhoneCallback.fail() "+errorCode);
			if (errorCode == ErrorCodes.AUTHENTICATION_FAIL || errorCode == ErrorCodes.SIP_PORTS_BUSY){
				flash_API.dropRegisteredTimer();
			}			
			flash_API.apiNotify.notifyError(errorCode,_sipObject);
		}
		
		public function close():void{
		}
		
		public function registered(_sipObject:Object):void{
			flash_API.apiNotify.notifyRegistered(_sipObject);
			CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.REGISTERED,flash_API));
		}
		
		public function ring(_call:Object,_sipObject:Object):void{
			var call:Call = process(_call);
			flash_API.apiNotify.notify(call,_sipObject);
			if (!call.incoming){
				CairngormEventDispatcher.getInstance().dispatchEvent(new CallEvent(CallEvent.OUT,call));
			} else {
				flash_API.apiNotify.notifyCall(call,_sipObject);
				CairngormEventDispatcher.getInstance().dispatchEvent(new CallEvent(CallEvent.IN,call));
			}
		}
		
		public function sessionProgress(_call:Object,_sipObject:Object):void{
			var call:Call = process(_call);
			CairngormEventDispatcher.getInstance().dispatchEvent(new CallEvent(CallEvent.SESSION_PROGRESS,call));
		}
		
		public function talk(_call:Object,_sipObject:Object):void{
			var call:Call = process(_call);
			flash_API.apiNotify.notify(call,_sipObject);
			CairngormEventDispatcher.getInstance().dispatchEvent(new CallEvent(CallEvent.TALK,call));
		}
		
		public function hold(_call:Object, _sipObject:Object):void{
			var call:Call = process(_call);
			flash_API.apiNotify.notify(call,_sipObject);
			CairngormEventDispatcher.getInstance().dispatchEvent(new CallEvent(CallEvent.HOLD,call));
		}
		
		// Notify about CIF 352x288 or QCIF 176x144 video format 
		public function notifyVideoFormat(_call:Object):void{
			var call:Call = process(_call);
			flash_API.apiNotify.notifyVideoFormat(call);			
			CairngormEventDispatcher.getInstance().dispatchEvent(new CallEvent(CallEvent.VIDEO_FORMAT_CHANGED, call));
		}
		
		public function callbackHold(_call:Object, isHold:Boolean):void{
			var call:Call = process(_call);
			call.iHolded = isHold;
			if (!isHold){
				for each (var tempCall:Call in flash_API.calls){
					if (tempCall.state == Call.STATE_TALK && tempCall.id != call.id){
						tempCall.setStatusHold(true);
					}
				}
			}
			flash_API.apiNotify.notifyCallbackHold(call,isHold);
		}
		public function busy(_call:Object,_sipObject:Object):void{
			var call:Call = process(_call);
			flash_API.apiNotify.notify(call,_sipObject);
			CairngormEventDispatcher.getInstance().dispatchEvent(new CallEvent(CallEvent.BUSY,call));	
		}
		
		public function finish(_call:Object,_sipObject:Object):void{
			var call:Call = process(_call);
			flash_API.apiNotify.notify(call,_sipObject);
			CairngormEventDispatcher.getInstance().dispatchEvent(new CallEvent(CallEvent.FINISH,call));
		}	
		
		public function process(_call:Object):Call{
			Logger.info("PhoneCallback.process() id="+_call.id+" state="+_call.state+" callee="+_call.callee);
			var call:Call = flash_API.getCallById(_call.id);
			if (call==null){
				call = new Call(flash_API);
				call.id = _call.id;	
			}

			if (_call.isIncomming == 'true'){
				call.incoming = true;
			}else{
				call.incoming = false;
			}
			if (_call.isVideoCall == 'true'){
				call.isVideoCall = true;
			}else{
				call.isVideoCall = false;
			} 
			call.callee = _call.callee;
			call.caller = _call.caller;
			if (call.incoming){
				call.anotherSideUser = _call.caller;
			}else{
				call.anotherSideUser = _call.callee;
			}
			call.visibleNameCallee = _call.visibleNameCallee;
			call.visibleNameCaller = _call.visibleNameCaller;
			call.state = _call.state;
			call.sip_state = _call.sip_state;
			call.playerVideoFormat = _call.playerVideoFormat;
			call.streamerVideoFormat = _call.streamerVideoFormat;
		
			flash_API.addCall(call);			
			Logger.info("PhoneCallback.process() complete id="+call.id+" state="+call.state+" callee="+call.callee);
			return call;
		}
		
	}
}